<template>
  <BasicModal v-bind="$attrs" @register="registerModal" :title="getTitle" @ok="handleSubmit">
    <BasicForm @register="registerForm" />
  </BasicModal>
</template>
<script lang="ts">
  import { defineComponent, ref, computed, unref } from 'vue';
  import { BasicModal, useModalInner } from '@/components/Modal';
  import { BasicForm, useForm } from '@/components/Form/index';

  import { getDeptList } from '@/api/demo/system';
  export default defineComponent({
    name: 'DeptModal',
    components: { BasicModal, BasicForm },
    emits: ['success', 'register'],
    setup(_, { emit }) {
      const isUpdate = ref(true);

      const [registerForm, { resetFields, setFieldsValue, updateSchema, validate }] = useForm({
        labelWidth: 100,
        baseColProps: { span: 24 },
        schemas: [
          {
            field: 'title',
            label: '标题',
            required: true,
            component: 'Input',
            colProps: { span: 24 },
          },
          {
            field: 'href',
            label: '链接',
            required: true,
            component: 'Input',
            colProps: { span: 24 },
          },
          {
            field: 'fontSize',
            label: '字体大小',
            // required: true,
            component: 'RadioButtonGroup',
            componentProps: {
              size:"small",
              defaultValue: '',
              options: [
                { label: '默认', value: '' },
                { label: '14px', value: '14px' },
                { label: '15px', value: '15px' },
                { label: '16px', value: '16px' },
                { label: '17px', value: '17px' },
                { label: '18px', value: '18px' },
                { label: '19px', value: '19px' },
                { label: '20px', value: '20px' },
              ],
            },
            colProps: { span: 24 },
          },
          {
            field: 'color',
            label: '文字颜色',
            // required: true,
            component: 'RadioButtonGroup',
            componentProps: {
              size:"small",
              defaultValue: '',
              options: [
                { label: '默认', value: '' },
                { label: 'Red', value: 'red' },
                { label: 'Pink', value: 'pink' },
                { label: 'Orange', value: 'orange' },
                { label: 'Green', value: 'green' },
                { label: 'Cyan', value: 'cyan' },
                { label: 'Blue', value: 'blue' },
                { label: 'Purple', value: 'purple' },
              ],
            },
            colProps: { span: 24 },
          },
          {
            field: 'bgColor',
            label: '背景颜色',
            // required: true,
            component: 'RadioButtonGroup',
            componentProps: {
              size:"small",
              defaultValue: '',
              options: [
                { label: '默认', value: '' },
                { label: 'Red', value: 'red' },
                { label: 'Pink', value: 'pink' },
                { label: 'Orange', value: 'orange' },
                { label: 'Green', value: 'green' },
                { label: 'Cyan', value: 'cyan' },
                { label: 'Blue', value: 'blue' },
                { label: 'Purple', value: 'purple' },
              ],
            },
            colProps: { span: 24 },
          },
        ],
        showActionButtonGroup: false,
      });

      const [registerModal, { setModalProps, closeModal }] = useModalInner(async (data) => {
        resetFields();
        setModalProps({ confirmLoading: false });
        isUpdate.value = !!data?.isUpdate;

        if (unref(isUpdate)) {
          setFieldsValue({
            ...data.record,
          });
        }
        const treeData = await getDeptList();
        updateSchema({
          field: 'parentDept',
          componentProps: { treeData },
        });
      });

      const getTitle = computed(() => (!unref(isUpdate) ? '新增链接' : '编辑链接'));

      async function handleSubmit() {
        try {
          const values = await validate();
          setModalProps({ confirmLoading: true });
          // TODO custom api
          // console.log(values);
          closeModal();
          emit('success',values);
        } finally {
          setModalProps({ confirmLoading: false });
        }
      }

      return { registerModal, registerForm, getTitle, handleSubmit };
    },
  });
</script>
